// src/utils/asyncRoutes.ts
import { RouteRecordRaw } from 'vue-router'

export interface AsyncRoute {
  path: string
  name?: string
  component?: string
  redirect?: string
  meta?: {
    title?: string
    icon?: string
    roles?: string[]
  }
  children?: AsyncRoute[]
}

// 动态加载组件
const lazyLoad = (componentPath: string) => {
  return () => import(/* @vite-ignore */ `/src/views/${componentPath}.vue`)
}

// 转换后端路由为Vue路由
export const transformRoutes = (routes: AsyncRoute[]): RouteRecordRaw[] => {
  return routes.map(route => {
    const vueRoute: RouteRecordRaw = {
      path: route.path,
      name: route.name,
      redirect: route.redirect,
      meta: route.meta,
      component: route.component 
        ? lazyLoad(route.component)
        : undefined,
      children: route.children 
        ? transformRoutes(route.children) 
        : []
    }
    return vueRoute
  })
}